import { createSlice } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";
import type { indexStoreType } from "@/typings/storeType";
import type { SeekResultType } from "@/utils/ScheduleManage";
import type { DeviceType } from "@/typings/businessType";

const indexSlice = createSlice({
  name: "index",
  initialState: (): indexStoreType => {
    return {
      isWebsocketConnect: false,
      entryNetworkInfo: null,
      curSchedule: [],
      curPlayData: {
        type: "POLYFILL",
        filePath: "",
        rawData: null,
        content: "",
        errorText: "",
        width: 0,
        height: 0
      },
      nextMaterial: null
    };
  },
  reducers: {
    setIsWebsocketConnect: (state, action: PayloadAction<boolean>) => {
      state.isWebsocketConnect = action.payload;
    },
    setEntryNetworkInfo: (state, action: PayloadAction<DeviceType>) => {
      state.entryNetworkInfo = action.payload;
    },
    setCurSchedule: (state, action: PayloadAction<SeekResultType["scheduleList"]>) => {
      state.curSchedule = action.payload;
    },
    setCurPlayData: (state, action: PayloadAction<indexStoreType["curPlayData"]>) => {
      state.curPlayData = action.payload;
    },
    setNextMaterial: (state, action: PayloadAction<indexStoreType["nextMaterial"]>) => {
      state.nextMaterial = action.payload;
    }
  }
});

export const { setCurSchedule, setIsWebsocketConnect, setCurPlayData, setEntryNetworkInfo, setNextMaterial } =
  indexSlice.actions;

export { indexSlice };
